
package com.probiz.estoremf.customer.web.action;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.web.servlet.ModelAndView;

import com.probiz.estore.common.model.order.OrderShipment;
import com.probiz.estore.common.model.order.SalesOrder;
import com.probiz.estore.common.service.OrderService;
import com.probiz.estore.core.controller.GenericStoreFrontController;
import com.probiz.estore.core.search.SearchCriteria;
import com.probiz.estore.exception.OrderProccessFlowException;
import com.probiz.estore.order.model.OrderSearchCriteria;
import com.probiz.estore.order.service.OrderProcessFlowManager;
import com.probiz.estore.order.service.OrderShipmentManager;
import com.probiz.estore.order.service.SalesOrderManager;
import com.probiz.estore.webapp.util.RequestContext;
import com.probiz.estore.webapp.util.RequestUtil;

/**
 * Salse Order Front Page Controller
 *  
 */
public class OrderFrontController extends GenericStoreFrontController<SalesOrder> {
    
	private OrderService orderService;
	private SalesOrderManager salesOrderManager;
    private OrderProcessFlowManager orderProcessFlowManager;
    private OrderShipmentManager orderShipmentManager;

	public void setOrderProcessFlowManager(
			OrderProcessFlowManager orderProcessFlowManager) {
		this.orderProcessFlowManager = orderProcessFlowManager;
	}

	public void setOrderService(OrderService orderService) {
		this.orderService = orderService;
	}

	

	public void setSalesOrderManager(SalesOrderManager salesOrderManager) {
		this.salesOrderManager = salesOrderManager;
	}

    
	public OrderService getOrderService() {
		return orderService;
	}

	
	
	public void setOrderShipmentManager(OrderShipmentManager orderShipmentManager) {
		this.orderShipmentManager = orderShipmentManager;
	}

	@Override
	protected void initController() throws Exception {
		mgr = salesOrderManager;
		
	}
	
	/**
	 * 查询订单
	 * 如果是登录用户，则直接进入订单列表；否则进入匿名查找订单页面
	 * @param request
	 * @param response
	 * @return
	 */
	public ModelAndView queryOrder(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mv;
		if(RequestContext.getCurrentUserId() > 0){
			mv = new ModelAndView("redirect:/customer/csOrders.html");
		}else{
			mv = new ModelAndView("customer/queryOrder4Anonymous");
		}
		return mv;
	}

	public ModelAndView defaultAction(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mv = new ModelAndView("customer/csOrderList");
		try {
			SearchCriteria searchCriteria = createSearchCriteria(request,SalesOrder.FilterHqlName.DEFAULT);
			if (searchCriteria.getPageSize() == defaultPageSize) {
				searchCriteria.changePaging(searchCriteria.getPageNo(), 2);
			}
			OrderSearchCriteria orderSearchCriteria = new OrderSearchCriteria();
			// 绑定参数
			bind(request, orderSearchCriteria);
			orderSearchCriteria.setCustomerId(RequestContext.getCurrentUserId());
			List<SalesOrder> orderList = salesOrderManager.searchByCriteriaForController(orderSearchCriteria, searchCriteria);
			mv.addObject("orderList", orderList);
			mv.addObject("orderSearchCriteria", orderSearchCriteria);
		} catch (Exception e) {
			e.printStackTrace(); 
		}

		return mv;
	}

	public ModelAndView search(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mv = new ModelAndView("customer/include/csOrderListBody");
		try {
			SearchCriteria searchCriteria = createSearchCriteria(request,SalesOrder.FilterHqlName.DEFAULT);
			if (searchCriteria.getPageSize() == defaultPageSize) {
				searchCriteria.changePaging(searchCriteria.getPageNo(), 2);
			}
			OrderSearchCriteria orderSearchCriteria = new OrderSearchCriteria();
			// 绑定参数
			bind(request, orderSearchCriteria);
			orderSearchCriteria.setCustomerId(RequestContext.getCurrentUserId());
			List<SalesOrder> orderList = salesOrderManager.searchByCriteriaForController(orderSearchCriteria, searchCriteria);
			mv.addObject("orderList", orderList);
			mv.addObject("orderSearchCriteria", orderSearchCriteria);
		} catch (Exception e) {
			e.printStackTrace(); 
		}

		return mv;
	}
 
    /**
     * 查看订单
     * @param request
     * @param response
     * @return
     * @throws IOException 
     */
	public ModelAndView viewOrder(HttpServletRequest request,HttpServletResponse response) throws IOException {
		SalesOrder salesOrder	=orderService.getSalesOrderByOrderNo4Safety(request.getParameter("orderNo"), RequestContext.getCurrentUserId());
		if(salesOrder == null){
			//找不到订单则抛出安全异常
			response.sendError(404);
		}
		request.setAttribute("order", salesOrder);
		return new ModelAndView("customer/csOrderReview");
	}
	
	/**
	 * 匿名查看订单
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	public ModelAndView viewOrder4Anonymous(HttpServletRequest request,HttpServletResponse response) throws IOException {
		String orderNo = request.getParameter("orderNo");
		SalesOrder salesOrder	= salesOrderManager.getSalesOrderByOrderNo(orderNo);
		request.setAttribute("order", salesOrder);
		request.setAttribute("orderNo", orderNo);
		request.setAttribute("isAnonymous", "true");
		return new ModelAndView("customer/csOrderReview4Anonymous");
	}
	
	
	
	/**
	 * 准备取消订单
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException 
	 */
	public ModelAndView toCancel(HttpServletRequest request, HttpServletResponse response) throws IOException {
		SalesOrder salesOrder	=orderService.getSalesOrderByOrderNo4Safety(request.getParameter("orderNo"), RequestContext.getCurrentUserId());
		if(salesOrder == null){
			//找不到订单则抛出安全异常
			response.sendError(404);
		}
		request.setAttribute("order", salesOrder);
		return new ModelAndView("customer/csCancelOrderForm");
	}
	

	/**
	 * 取消订单
	 * @param request
	 * @param response
	 * @return
	 */
	public ModelAndView cancleOrder(HttpServletRequest request,	HttpServletResponse response) {
		SalesOrder salesOrder = orderService.getSalesOrderByOrderNo4Safety(request.getParameter("orderNo"), RequestContext.getCurrentUserId());
		String cancelReason = RequestUtil.getParameterNullSafe(request, "cancelReason");
		String cancelReasonPre = RequestUtil.getParameterNullSafe(request, "cancelReasonPre");
		if(StringUtils.isNotBlank(cancelReason)){
			salesOrder.setCancelReason(cancelReasonPre+","+cancelReason);
		}else{
			salesOrder.setCancelReason(cancelReasonPre);
		}
		String msg = getMessage("salesOrder.cancel.success", new Object[]{salesOrder.getOrderNo()});
		String errorMsg = getMessage("salesOrder.cancel.error", new Object[]{salesOrder.getOrderNo()});
		try {
			orderProcessFlowManager.doCancelOrderByCustomer(salesOrder);
			saveMessage(request, msg);
		} catch (OrderProccessFlowException e) {
			e.printStackTrace();
			saveErrorMessage(request,errorMsg);
		} catch (Exception e) {
			e.printStackTrace();
			saveErrorMessage(request,errorMsg);
		}
		
		return this.getRedirectView("/customer/csOrders.html");
		
	}
	

	/**
	 * 查询快递100
	 * @param request
	 * @param response
	 * @return
	 */
	public ModelAndView queryKuaidi100(HttpServletRequest request, HttpServletResponse response){
		String documentNo = request.getParameter("documentNo");
		Short flag = orderShipmentManager.queryKuaidi100(documentNo);
		OrderShipment orderShipment = orderShipmentManager.getByDocumentNo(documentNo);
		ModelAndView mv = new ModelAndView("/customer/include/csOrderReview4Kuaidi100Message");
		mv.addObject("orderShipment", orderShipment);
		mv.addObject("flag", flag);
		return mv;
	}
	

}